package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.enums.EventType;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.modules.admin.service.AlarmEventService;
import org.springblade.modules.admin.vo.AlarmEventAdminVO;
import org.springblade.modules.admin.vo.AlarmEventVO;
import org.springframework.web.bind.annotation.*;

import static org.springblade.modules.admin.service.AlarmEventService.EVENT_CONFIRM_STATUS;


/**
 * @Description 上报事件管理端业务接口
 * @Author ysq
 * @Date 2021/6/2
 **/
@RestController
@AllArgsConstructor
@RequestMapping("admin/event")
@Api(value = "上报事件管理端业务接口", tags = "上报事件管理端业务接口")
public class AlarmEventController {

	private final AlarmEventService alarmEventService;

	/**
	 * 自定义分页 上报事件分页
	 */
	@TenantIgnore
	@GetMapping("/alarmEventPage")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "上报事件分页", notes = "")
	public R<IPage<AlarmEventVO>> alarmEventPage(AlarmEventAdminVO alarmEventAdminVO, Query query, BladeUser bladeUser) {
		if (!BladeConstant.ADMIN_TENANT_ID.equals(bladeUser.getTenantId())) {
			alarmEventAdminVO.setTenantId(bladeUser.getTenantId());
		}
		IPage<AlarmEventVO> pages = alarmEventService.selectAlarmEventAdminPage(Condition.getPage(query), alarmEventAdminVO);
		return R.data(pages);
	}

	@TenantIgnore
	@PostMapping("/eventConfirm/{eventId}")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "事件确认")
	public R eventConfirm(@PathVariable("eventId") Long eventId) {
		AlarmEventVO alarmEvent = alarmEventService.getAlarmEventDetailById(eventId);
		if (alarmEvent == null) {
			return R.fail("数据异常！");
		}
		if (alarmEvent.getStatus() == EVENT_CONFIRM_STATUS) {
			return R.fail("事件已确认，不能重复确认！");
		}
		return R.status(alarmEventService.eventConfirm(alarmEvent));
	}

	@GetMapping("/eventTypeList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "事件类型下拉列表")
	public R eventTypeList() {
		return R.data(EventType.eventTypeMap());
	}
}
